from brian import *
from brian.library.random_processes import *
from brian.library.synapses import  *
import mydata as mydat
import NeuronFactory_IN as FactoryIN
import NeuronFactory_INRG as FactoryINRG
import NeuronFactory_MN as FactoryMN
import NeuronFactory_PF as FactoryPF
import NeuronFactory_RG as FactoryRG

def spToList(a):
    l = list()
    
    for x in a.spiketimes:
        l+=((a[x]/20*1000/md.BINSIZE)/ms).tolist()
    return l

#defaultclock.dt = 0.0025*ms

md=mydat.globVars()
md.DUR=5000

#myfacIN = FactoryIN.NeuronFactory()
myfacINRG = FactoryINRG.NeuronFactory()
#myfacMN = FactoryMN.NeuronFactory()
#myfacPF = FactoryPF.NeuronFactory()
myfacRG = FactoryRG.NeuronFactory()


#P=myfac.generateIN(1)
#P=myfac.generateMN(1,-61.4557,-59.835)
P=myfacRG.generate(40)
PF=P.subgroup(20)
PE=P.subgroup(20)

IN=myfacINRG.generate(40)

INE=IN.subgroup(20)
INF=IN.subgroup(20)

print md.gsynapse

#connections between Pattern generation neurons to inhibitory interneurons
CinE=Connection(PE,INF,'ge',weight=md.gsynapse*0.45,sparseness=1,delay=lambda i,j:7*ms+randn(len(PE))*2*ms);
CinF=Connection(PF,INE,'ge',weight=md.gsynapse*0.45,sparseness=1,delay=lambda i,j:7*ms+randn(len(PE))*2*ms);

#self potentiation of pattern generation neurons
Cs1=Connection(PE,PE,'ge',weight=md.gsynapse*0.0125,sparseness=1,delay=lambda i,j:2*ms+randn(len(PE))*0.3*ms);
Cs2=Connection(PF,PF,'ge',weight=md.gsynapse*0.0125,sparseness=1,delay=lambda i,j:2*ms+randn(len(PF))*0.3*ms);

#reciprocal potentiation of pattern generation neurons
Cr1=Connection(PE,PF,'ge',weight=md.gsynapse*0.0125,sparseness=1,delay=lambda i,j:14*ms+randn(len(PE))*0.6*ms);
Cr2=Connection(PF,PE,'ge',weight=md.gsynapse*0.0125,sparseness=1,delay=lambda i,j:14*ms+randn(len(PF))*0.6*ms);

#inhibition of pattern generation neurons
CinE2=Connection(INE,PE,'gi',weight=md.gsynapse*0.115,sparseness=1,delay=lambda i,j:7*ms+randn(len(PE))*2*ms);
CinF2=Connection(INF,PF,'gi',weight=md.gsynapse*0.115,sparseness=1,delay=lambda i,j:7*ms+randn(len(PE))*2*ms);

FREQ=30*Hz
spiketimes = [(0,5*ms),(1,5*ms),(2,5*ms),(3,5*ms),(4,5*ms),(5,5*ms),(6,5*ms),(7,5*ms),(8,5*ms),(9,5*ms),(10,5*ms),(11,5*ms),(12,5*ms),(13,5*ms),(14,5*ms),(15,5*ms),(16,5*ms),(17,5*ms),(18,5*ms),(19,5*ms),]

    
SG = SpikeGeneratorGroup(20,spiketimes,period=1/FREQ)
CINPUT1=Connection(SG,PE,'ge',weight=md.gsynapse*0.08,sparseness=1,delay=lambda i,j:15*ms+randn(len(PE))*4*ms);
CINPUT2=Connection(SG,PF,'ge',weight=md.gsynapse*0.075,sparseness=1,delay=lambda i,j:15*ms+randn(len(PE))*4*ms);

tracePF=StateMonitor(PF,'vm',record=[0])
tracePFgi=StateMonitor(PF,'gi_current',record=[0])
tracePFge=StateMonitor(PF,'ge_current',record=[0])
tracePEgi=StateMonitor(PE,'gi_current',record=[0])
tracePEge=StateMonitor(PE,'ge_current',record=[0])
tracePE=StateMonitor(PE,'vm',record=[0])
traceINE=StateMonitor(INE,'vm',record=[0])
traceINF=StateMonitor(INF,'vm',record=[0])

spikesPF = SpikeMonitor(PF)
spikesPE = SpikeMonitor(PE)
spikesINE= SpikeMonitor(INE)
spikesINF= SpikeMonitor(INF)
myfacRG.initNet(P)
myfacINRG.initNet(IN)
#traceD=StateMonitor(P,'vmd',record=[0])
net = Network(P, IN, SG, 
              CinE, CinF, Cs1, Cs2, Cr1, Cr2, CinE2, CinF2, CINPUT1, CINPUT2, 
              tracePF, tracePFgi, tracePFge,tracePE, traceINE, traceINF, tracePEge, tracePEgi,
              spikesPF, spikesPE, spikesINE, spikesINF)

net.run(md.DUR*msecond)
figure(1)
subplot(411)
plot(tracePF.times/ms,tracePF[0]/mV)
#plot(tracePFgi.times/ms,tracePFgi[0]/nA)
#plot(tracePFge.times/ms,tracePFge[0]/nA)

subplot(412)
plot(tracePE.times/ms,tracePE[0]/mV)
#plot(tracePEgi.times/ms,tracePEgi[0]/nA)
#plot(tracePEge.times/ms,tracePEge[0]/nA)
subplot(413)
plot(traceINE.times/ms,traceINE[0]/mV)
subplot(414)
plot(traceINF.times/ms,traceINF[0]/mV)



figure(2)
subplot(411)
#raster_plot(spikesPE)
x = spToList(spikesPF)
hist(x,floor(md.DUR/md.BINSIZE),range=[0,md.DUR],histtype='step')

subplot(412)
x = spToList(spikesPE)
hist(x,floor(md.DUR/md.BINSIZE),range=[0,md.DUR],histtype='step')
subplot(413)
x = spToList(spikesINE)
hist(x,floor(md.DUR/md.BINSIZE),range=[0,md.DUR],histtype='step')
subplot(414)
x = spToList(spikesINF)
hist(x,floor(md.DUR/md.BINSIZE),range=[0,md.DUR],histtype='step')
show()

